package com.mihong.handler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.mihong.entity.Frame;
import com.mihong.entity.Message;

/**
 * 	数据处理
 * @date: 2019年3月5日
 */
public class Handler {

	private List<byte[]> frames = new ArrayList();
	
	/**
	 * 	存储数据帧
	 * @param data
	 * @param success
	 */
	public List<byte[]> handler(String data,boolean success) {
		
		Frame frame = surfaceDataHandler(data);
		
		String dataBody = "";
		//获取版本、帧序号、网关mac、命令码、数据长度字段字节数
		int length = (Message.BYTE_VERSION+Message.BYTE_FRAME_NO+Message.BYTE_MAC
				+Message.BYTE_COMMAND_CODE+Message.BYTE_DATA_BODY_LENGTH)*Message.LENGTH_STRING_HEX;
		if(!(data.length() == length)) {//判断是否有数据体
			
			dataBody = data.substring(length);
		}
		
		String command = data.substring(16,18);
		byte[] frameBuf = selectHandler(command,dataBody,success,frame);
		
		if(frameBuf != null) {
			frames.add(frameBuf);
		}
		
		return frames;
	}
	
	/**
	 * 	表层数据处理(版本、帧序号、地址、命令码、数据长度)
	 * @param data
	 * @return 有效数据
	 */
	private Frame surfaceDataHandler(String data) {
		
		String version = data.substring(Message.START_INDEX, Message.LENGTH_STRING_HEX*Message.BYTE_VERSION);
		String frameNo = data.substring(Message.LENGTH_STRING_HEX*Message.BYTE_VERSION, Message.LENGTH_STRING_HEX*(Message.BYTE_VERSION+Message.BYTE_FRAME_NO));
		String address = data.substring(Message.LENGTH_STRING_HEX*(Message.BYTE_VERSION+Message.BYTE_FRAME_NO), Message.LENGTH_STRING_HEX*(Message.BYTE_VERSION+Message.BYTE_FRAME_NO+Message.BYTE_ADDRESS));
		String command = data.substring(Message.LENGTH_STRING_HEX*(Message.BYTE_VERSION+Message.BYTE_FRAME_NO+Message.BYTE_ADDRESS),Message.LENGTH_STRING_HEX*(Message.BYTE_VERSION+Message.BYTE_FRAME_NO+Message.BYTE_ADDRESS+Message.BYTE_COMMAND_CODE));
		String length = data.substring(Message.LENGTH_STRING_HEX*(Message.BYTE_VERSION+Message.BYTE_FRAME_NO+Message.BYTE_ADDRESS+Message.BYTE_COMMAND_CODE), 
				Message.LENGTH_STRING_HEX*(Message.BYTE_VERSION+Message.BYTE_FRAME_NO+Message.BYTE_ADDRESS+Message.BYTE_COMMAND_CODE+Message.BYTE_DATA_BODY_LENGTH));
		String crc = "";
		
		//构建数据帧
		Frame frame = new Frame();
		frame.setCommand(command);
		frame.setLength(length);
		frame.setAddress(address);;
		frame.setFrameNo(frameNo);
		frame.setVersion(version);
		frame.setDataBody(new HashMap<String,Object>());
		frame.setCrc(crc);
		
		return frame;
	}
	
	/**
	 * 	处理接收并返回应答帧
	 * @param dataBody
	 * @param success
	 * @param frame
	 * @return 应答帧
	 */
	private byte[] selectHandler(String commandCode,String dataBody,Boolean success,Frame frame) {
		
		byte[] frameBuf = null;//回应数据
		
		if(Message.COMMAND_REGISTER.equalsIgnoreCase(commandCode)) {//注册包
			//接收并应答数据
			 new ReceiveHandler().registerHandler(dataBody,success,frame);
			 frameBuf = new ReplayHandler().registerHandler(frame,success);
		  }else if(Message.COMMAND_HEART_BEAT.equals(commandCode)) {//心跳数据帧
	    	  new ReceiveHandler().heartBeatHandler(dataBody,success,frame);
	    	  frameBuf = new ReplayHandler().heartBeatHandler(frame,success);

	      }else if(Message.COMMAND_STATUS.equals(commandCode)) {//终端状态数据帧
	    	  String equimentsMAC = new ReceiveHandler().statusHandler(dataBody,success,frame);
	    	  frameBuf = new ReplayHandler().statusHandler(frame,success);
	    	//1 服务器发送数据帧
	    	  //1.1 发送的设备列表数据帧
	    	  byte[] frameEquipmentLis = new ReplayHandler().equipmentListHandler(frame, equimentsMAC);
	    	  frames.add(frameEquipmentLis);
	    	  //1.2 发送的ip地址切换数据帧
	    	  byte[] frameIpSwitching = new ReplayHandler().ipSwitchingHandler(frame);
	    	  frames.add(frameIpSwitching);
	    	  
	      }else if(Message.COMMAND_EQUIPMENT_LIST.equals(commandCode)) {//设备列表数据帧
	    	//存储网关数据
	    	  new ReceiveHandler().equipmentListHandler(dataBody,success, frame);
	      }else if(Message.COMMAND_IP_SWITCHING.equalsIgnoreCase(commandCode)) {//ip切换数据帧
	    	//存储网关数据
	    	  new ReceiveHandler().ipSwitchingHandler(dataBody,success,frame);
	      
	      }
		
		return frameBuf;
	}

	
	
}
